<?php

namespace Payment\Model;

use Common\Model\BaseModel;
use Common\Model\ModelException;

class ChanrgeModel extends BaseModel {

    protected $_auto = [
        ['create_time', 'time', self::MODEL_INSERT, 'function'],
        ['update_time', 'time', self::MODEL_BOTH, 'function'],
    ];
    protected $_validate = [];
    protected $_scope = [];

    protected function _after_find(&$record, $options) {
        
    }

    protected function _after_select(&$records, $options) {
        foreach ($records as &$record) {
            $this->_after_find($record, $options);
        }
    }

    /**
     * 处理订单
     *
     * @param array data 
     * @return bool
     */
    public function deal($callbackData) {
        $chanrgeModel = D('Chanrge');
        $payment = $chanrgeModel->getByOnum($callbackData['order_no']);
        switch ($payment['moneyall']) {
            case 50:
                $freegold = 3000;  //送的金币
                break;
            case 100:
                $freegold = 8000;
                break;
            case 200:
                $freegold = 20000;
                break;
            default:
                $freegold = 0;
                break;
        }
        if ($payment['status'] == 'unpaid') {
            $uid = $payment['uid'];
            $Model = M();
            $Model->startTrans();
            //改变订单状态
            $data['status'] = 'paid';
            $data['id'] = $payment['id'];
            $data['transaction_id'] = $callbackData['transaction_id']?$callbackData['transaction_id']:'';
            $res = $chanrgeModel->setField($data);
            $GoldLogModel = D('Account/GoldLog');
            $userModel = D('User');
            $goldlog['user_id'] = $payment['uid'];
            $goldlog['gold'] = $payment['moneyall'] * 100;
            $addtime = time();
            
            switch ($payment['mark']) {
                case 'charge':
                    $gold = $freegold + $payment['moneyall'] * 100;
                    $res1 = $userModel->where(['id' => $payment['uid']])->setInc('gold', $gold); //增加用户金币
                    $goldlog['mark'] = '充值书币';
                    $goldlog['type'] = $payment['mark'];
                    $goldlog['add_time'] = $addtime;
                    $res2 = $GoldLogModel->addGoldLog($goldlog); //充值记录
                    if(!empty($freegold)){
                        $res3 = $GoldLogModel->addGoldLog(['user_id'=>$goldlog['user_id'],'gold'=>$freegold,'mark'=>'充值赠送书币','type'=>'send','add_time'=>$addtime]); //赠送记录
                    }
                    if (!empty($res) && !empty($res1) && !empty($res2)) {
                        /*我的任务*/
                        D('Task/task')->taskOnceRedis($uid,'task_once'.$uid, 4);//4代表的任务id 4
                        $Model->commit();
                        return true;
                    } else {
                        $Model->rollback();
                        return false;
                    }
                    break;
                case 'year_vip':
                    $goldlog['mark'] = '充值购买年度会员';
                    $goldlog['type'] = $payment['mark'];
                    $goldlog['add_time'] = $addtime;
                    $goldlog['gold'] = 0;
                    $user = $userModel->find($uid);
                    if($user['vip']==2){
                        $viptime =  $user['viptime'] + (365 * 24 * 60 * 60);
                        $res1 = $userModel->setField(['id'=>$uid,'viptime'=>$viptime]);
                    }else{
                        $viptime = time() + (365 * 24 * 60 * 60);
                        $res1 = $userModel->setField(['id'=>$uid,'vip' => 2, 'viptime' =>$viptime]);
                    }
                    if (!empty($res) && !empty($res1)) {
                        $vipendtime = date('Y-m-d',$viptime);
                        D('Account/Message')->addMessage('会员充值成功提醒',"您已经为金榜藏书APP年度会员,会员截止日期为{$vipendtime}",$uid,3);
                        $Model->commit();
                        return true;
                    } else {
                        $Model->rollback();
                        return false;
                    }
                    break;
                case 'vip':
                    $goldlog['mark'] = '充值购买季度会员';
                    $goldlog['type'] = $payment['mark'];
                    $goldlog['add_time'] = $addtime;
                    $goldlog['gold'] = 0;
                    $user = $userModel->find($uid);
                     if($user['vip']==2){
                        $viptime =  $user['viptime'] + (92 * 24 * 60 * 60);
                        $res1 = $userModel->setField(['id'=>$uid,'viptime'=>$viptime]);
                     }else{
                        $viptime = time() + (92 * 24 * 60 * 60);
                        $res1 = $userModel->setField(['id'=>$uid,'vip' => 2, 'viptime' =>$viptime]);
                     }
//                    $res2 = $GoldLogModel->addGoldLog($goldlog); //充值记录
                    if (!empty($res) && !empty($res1)) {
                        $vipendtime = date('Y-m-d',$viptime);
                        D('Account/Message')->addMessage('会员充值成功提醒',"您已经为金榜藏书APP季度会员,会员截止日期为{$vipendtime}",$uid,3);
                        $Model->commit();
                        return true;
                    } else {
                        $Model->rollback();
                        return false;
                    }
                    break;
                default:
                    return false;
            }
        }
    }
  
}
